Attribute VB_Name = "MODapplicationMain"
Option Explicit

' Declare Functions ---------------------------------------------------------------------------------------------------------------------------------

Private Declare Sub GetSystemTime Lib "Kernel32" (oSystemTime As tSystemTime)

' Objects ---------------------------------------------------------------------------------------------------------------------------------

Private fso As Scripting.FileSystemObject
Private oLog As Scripting.TextStream

' Types -----------------------------------------------------------------------------------------------------------------------------------

Private Type tSystemTime

    iYear As Integer
    iMonth As Integer
    iDayOfWeek As Integer
    iDay As Integer
    iHour As Integer
    iMinute As Integer
    iSecond As Integer
    iMilliseconds As Integer
    
End Type

Private oSystemTime As tSystemTime

' Constants -------------------------------------------------------------------------------------------------------------------------------

Public Const iERROR As Integer = 1
Public Const iWARNING As Integer = 2
Public Const iINFO As Integer = 3
Public Const iDEBUG As Integer = 4

' Strings ---------------------------------------------------------------------------------------------------------------------------------

Dim _
    sModule As String, _
    sMessage As String, _
    sData As String, _
    sVBerrorObjectDescription As String

' Integers ----------------------------------------------------------------------------------------------------------------------------

Dim iLevel As Integer

' Longs --------------------------------------------------------------------------------------------------------------------------------------

Dim lVBerrorObjectNumber As Long

' begin code -----------------------------------------------------------------------------------------------------------------------------
' Logger function ----------------------------------------------------------------------------------------------------------------------------

Public Sub logMessage( _
    iLevel As Integer, _
    sModule As String, _
    sMessage As String, _
    Optional sData As String, _
    Optional lVBerrorObjectNumber As Long, _
    Optional sVBerrorObjectDescription As String)

    Dim sDateTime As String
    
    sDateTime = Date & " " & Time

    Set fso = New Scripting.FileSystemObject
    
    Set oLog = fso.OpenTextFile(sLogFilePath, ForAppending, True)

    oLog.WriteLine _
        iLevel & vbTab & _
        Date & " " & Time & vbTab & _
        sModule & vbTab & _
        sMessage & vbTab & _
        Replace(sData, vbCrLf, "<LineBreak>") & vbTab & _
        lVBerrorObjectNumber & vbTab & _
        sVBerrorObjectDescription
        
    oLog.Close
    
    Set oLog = Nothing
    
    Set fso = Nothing
        
End Sub

' Check version in registry and update registry if necessary -------------------------------------------------------------------

Public Function checkVersion()

    With New regOp
    
        .Root = HKEY_LOCAL_MACHINE
        .Key = "Software\bbd\AccessSearch"
        
        If .Value("version") <> "4.2" Then
            .Value("version") = "4.2"
            .Key = "Software\bbd\AccessSearch\preferences"
            .Value("columnWidths") = ""
            .Value("defaultDatabasePath") = App.Path & "\localStock.mdb"
            .Value("defaultSearchField") = "partNumber"
            .Key = "Software\bbd\AccessSearch\preferences\remoteDB"
            .Value("autoUpdate") = 1
            .Value("fieldNames") = "partNumber,quantity,remark,manufacturer,dateCode"
            .Value("serverAddress") = "example : http://SERVER_ADDRESS"
            .Value("updatePagePath") = "example : FOLDER(s)/PAGE_NAME"
            .Value("databaseName") = "webStock.mdb"
        End If
        
    End With

End Function

' Create Unique Time stamp -------------------------------------------------------------------------------------------------------------------

Public Function getUniqueTimeStamp() As String
    
    ' call to API function to get current time

    GetSystemTime oSystemTime

    getUniqueTimeStamp = _
        Year(Now) & "_" & _
        Month(Now) & "_" & _
        Day(Now) & "_" & _
        Hour(Now) & "_" & _
        Minute(Now) & "_" & _
        Second(Now) & "_" & _
        oSystemTime.iMilliseconds

End Function

' Properties --------------------------------------------------------------------------------------------------------------------------------

Property Get sLogFilePath() As String

    sLogFilePath = App.Path & "/AccessSearch.log"

End Property


